package com.jkl.webserver.Tool;

import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

//该类用于处理文件的复制
//以单例模式注入
@Component
public class CopyFile
{
    public CopyFile(){};
    public void charCopy(Reader in,Writer out) throws IOException {//字符缓冲流(基类)
        BufferedReader br=new BufferedReader(in);
        BufferedWriter bw=new BufferedWriter(out);
        String buffer;
        while ((buffer=br.readLine())!=null){
            bw.write(buffer);//读一行
            bw.newLine();  // 跳到下一行
        }
        br.close();
        bw.close();
    }
    public void charCopy(String inPath,String outPath) throws IOException {//根据输入输出路径来进行复制
        charCopy(new FileReader(inPath),new FileWriter(outPath));
    }
    public void charCopy(File infile,File outfile) throws IOException {//根据输入输出File来进行复制
        charCopy(new FileReader(infile),new FileWriter(outfile));
    }

    public void charCopy(File infile,String outPath) throws IOException {
        charCopy(new FileReader(infile),new FileWriter(outPath));
    }
    public void charCopy(String inPath,File outfile) throws IOException {
        charCopy(new FileReader(inPath),new FileWriter( outfile));
    }
    public void charCopy(File infile,Writer writer) throws IOException {
        if (writer == null) {
            throw new IllegalArgumentException("Streams cannot be null");
        }
        charCopy(new FileReader(infile),writer);
    }
    public void charCopy(Reader in,File outfile) throws IOException {
        if (in == null) {
            throw new IllegalArgumentException("Streams cannot be null");
        }
        charCopy(in,new FileWriter(outfile));
    }
    public void charCopy(String inPath,Writer writer) throws IOException {//根据输入路径和
        if (writer == null) {
            throw new IllegalArgumentException("Streams cannot be null");
        }
        charCopy(new FileReader(inPath),writer);
    }
    public void charCopy(Reader in,String outPath) throws IOException {//字符流
        if (in == null) {
            throw new IllegalArgumentException("Streams cannot be null");
        }
        charCopy(in,new FileWriter(outPath));
    }

    public void byteCopy(InputStream is,OutputStream os) throws IOException//缓冲字节流读写(基类)
    {
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) != -1)
        {
            bos.write(buffer, 0, len);
        }
        bis.close();
        bos.close();
    }
    public void byteCopy(String inPath,String outPath) throws IOException {
        byteCopy(Files.newInputStream(Paths.get(inPath)), Files.newOutputStream(Paths.get(outPath)));
    }
    public void byteCopy(File infile,File outfile) throws IOException {
        byteCopy(Files.newInputStream(infile.toPath()), Files.newOutputStream(outfile.toPath()));
    }

    public void byteCopy(File infile,String outPath) throws IOException {
        byteCopy(Files.newInputStream(infile.toPath()), Files.newOutputStream(Paths.get(outPath)));
    }
    public void byteCopy(String inPath,File outfile) throws IOException {
        byteCopy(Files.newInputStream(Paths.get(inPath)), Files.newOutputStream(outfile.toPath()));
    }
    public void byteCopy(File infile,OutputStream os) throws IOException {
        if (os == null) {
            throw new IllegalArgumentException("Streams cannot be null");
        }
        if(infile.exists()){
            byteCopy(Files.newInputStream(infile.toPath()), os);
        }
    }
    public void byteCopy(InputStream is,File outfile) throws IOException {
        if (is == null) {
            throw new IllegalArgumentException("Streams cannot be null");
        }
        byteCopy(is, Files.newOutputStream(outfile.toPath()));
    }
    public void  byteCopy(String inPath,OutputStream os) throws IOException //字节流
    {
        if (os == null) {
            throw new IllegalArgumentException("Streams cannot be null");
        }
        byteCopy(Files.newInputStream(Paths.get(inPath)),os);
    }
    public void  byteCopy(InputStream is,String outPath) throws IOException //字节流
    {
        if (is == null) {
            throw new IllegalArgumentException("Streams cannot be null");
        }
        byteCopy(is,Files.newOutputStream(Paths.get(outPath)));
    }

    public void CopyALLFile(String inPath,String outPath){
        Path sourceDirectory = Paths.get(inPath);
        Path targetDirectory = Paths.get(outPath);
        try {
            Files.walk(sourceDirectory)
                    .filter(Files::isRegularFile)
                    .forEach(sourcePath -> {
                        Path targetPath = targetDirectory.resolve(sourcePath.getFileName());
                        try {
                            Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void CopyALLFile(File infile,File outfile){
        CopyALLFile(infile.getAbsolutePath(),outfile.getAbsolutePath());
    }
}